import pyperclip
from typing import List


class MarkdownTableBuilder:
    """将每两行作为一个词条生成 Markdown 表格"""

    def __init__(self, header: str = "用语说明"):
        """
        header: 表格表头列名
        """
        self.header = header
        self.entries: List[str] = []

    def read_entries(self):
        """从控制台读取词条，每两行为一个词条，空行结束"""
        print("请输入内容，每两行为一个词条（输入空行结束）：")
        while True:
            title = input().strip()
            if title == "":
                break
            description = input().strip()
            if description == "":
                break
            # 清理描述中的多余空格和换行
            description = description.replace("　", "").replace("\n", "").replace(" ", "")
            self.entries.append(f"{title}\n{description}")

    def build_markdown(self) -> str:
        """生成 Markdown 表格"""
        markdown_lines = [f"| {self.header} |", "|----------|"]
        for entry in self.entries:
            markdown_lines.append(f"| {entry} |")
        return "\n".join(markdown_lines)

    def process_and_copy(self) -> str:
        """生成 Markdown 并复制到剪贴板"""
        markdown_result = self.build_markdown()

        return markdown_result


if __name__ == "__main__":
    builder = MarkdownTableBuilder()
    builder.read_entries()
    result = builder.process_and_copy()
    print("\n以下是 Markdown 表格格式：\n")
    print(result)
    print("\n✅ 已复制到剪贴板！")
